import { TitleLevel } from '../../editor'

/**
 * 初始化标题功能
 * @param editor 编辑器实例
 * @param isApple 是否为苹果设备
 */
export default function initTitle(editor: any, isApple: boolean): void {
  const titleDom = document.querySelector<HTMLDivElement>('.menu-item__title')!
  const titleOptionDom = titleDom.querySelector<HTMLDivElement>('.options')!
  titleOptionDom.querySelectorAll('li').forEach((li, index) => {
    li.title = `Ctrl+${isApple ? 'Option' : 'Alt'}+${index}`
  })

  titleDom.onclick = function () {
    console.log('title')
    titleOptionDom.classList.toggle('visible')
  }
  titleOptionDom.onclick = function (evt) {
    const li = evt.target as HTMLLIElement
    const level = <TitleLevel>li.dataset.level
    editor.command.executeTitle(level || null)
  }
}